package com.aube.interact.model;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huyaonan on 15/10/24.
 */
public class TimeLineModel extends BaseModel {

    public TimeLineData data;

    public String getVideoId() {
        return data == null ? "" : data.videoid;
    }

    public static TimeLineModel parse(String s) {
        TimeLineModel result = new TimeLineModel();
        try {
            JSONObject jsonObject = new JSONObject(s);
            JSONObject dataObject = jsonObject.optJSONObject("data");
            TimeLineData data = new TimeLineData();
            data.videoid = dataObject.optString("videoid");
            data.videono = dataObject.optString("videono");
            data.starttime = dataObject.optString("starttime");
            data.endtime = dataObject.optString("endtime");
            data.title = dataObject.optString("title");
            data.themeColor = dataObject.optString("themeColor");

            JSONArray jsonArray = dataObject.optJSONArray("timeline");
            if(jsonArray != null) {
                List<TimeLinePiece> timeLinePieces = new ArrayList<>();
                for(int i=0; i<jsonArray.length(); i++) {
                    timeLinePieces.add(TimeLinePiece.parse(jsonArray.optJSONObject(i)));
                }
                data.timeline = timeLinePieces;
            }
            result.data = data;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
